package org.ilovecode.display {

	import events.LoadingEvent;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import org.ilovecode.data.GalleryRepository;
	import org.ilovecode.events.GalleryEvent;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class Gallery extends MovieClip {
		
		private var _repository:GalleryRepository;
		private var _imageContainer:BoundContainer;
		private var _thumbContainer:ScrollableContainer;
		
		private var _path:String = "galleryItems.xml";
		private var _thumbnailsPosition:String = "right";
		private var _thumbnailsContainerWidth:Number = 200;
		private var _thumbnailsContainerHeight:Number = 500;
		
		private var _width:Number = -1;
		private var _height:Number = -1;
		private var _specialLoadingStructure:String = "none";
		
		private var _initialShow:Boolean = true;
		
		public function Gallery(width:Number, height:Number, specialLoadingStructure:String = "none", initialShow:Boolean = true) {
			_width = width;
			_height = height;
			_specialLoadingStructure = specialLoadingStructure;
			_initialShow = initialShow;
			
			//_repository = new GalleryRepository(_path);
			//_repository.addEventListener(Event.COMPLETE, repository_Complete);
		}
		
		//{ region Handlers
		
		private function repository_Complete(event:Event):void {
			configLayout();
			
			_thumbContainer.setContent(_repository.getAllThumbnails());
			_thumbContainer.addEventListener(GalleryEvent.ITEM_CLICK, thumbContainer_ItemClick);
			
			if (_initialShow) {
				var image:Sprite = _repository.getImageByIndex(0);
				_imageContainer.setContent(image);
				this.dispatchEvent(new GalleryEvent(GalleryEvent.ITEM_CLICK, image.name));
			}
			
			this.dispatchEvent(new LoadingEvent(LoadingEvent.HIDE_PROGRESS_ICON));
		}
		
		private function thumbContainer_ItemClick(event:GalleryEvent):void {
			this.dispatchEvent(event.clone());
			var image:Sprite = _repository.getImageByName(event.name);
			_imageContainer.setContent(image);
		}
		
		//} endregion
		
		private function configLayout():void {
			var SPACING:int = 3;
			
			switch (thumbnailsPosition) {
				case "right":
					_thumbContainer = new ScrollableContainer(thumbnailsContainerWidth, thumbnailsContainerHeight, "vertical");
					_thumbContainer.x = _width - thumbnailsContainerWidth;
					_thumbContainer.y = _height - thumbnailsContainerHeight;
					//TODO: Change default BR to user choice
					_imageContainer = new BoundContainer(_width - thumbnailsContainerWidth - SPACING, _height, "BR");
					_imageContainer.x = 0;
					_imageContainer.y = 0;
					break;
				case "left":
					_thumbContainer = new ScrollableContainer(thumbnailsContainerWidth, thumbnailsContainerHeight, "vertical");
					_thumbContainer.x = 0;
					_thumbContainer.y = _height - thumbnailsContainerHeight;
					//TODO: Change default BR to user choice
					_imageContainer = new BoundContainer(_width - thumbnailsContainerWidth - SPACING, _height, "BL");
					_imageContainer.x = thumbnailsContainerWidth + SPACING;
					_imageContainer.y = 0;
					break;
				case "up":
					_thumbContainer = new ScrollableContainer(thumbnailsContainerWidth, thumbnailsContainerHeight, "horizontal");
					break;
				case "down":
					_thumbContainer = new ScrollableContainer(thumbnailsContainerWidth, thumbnailsContainerHeight, "horizontal");
					break;
				default:
					break;
			}
			
			this.addChild(_imageContainer);
			this.addChild(_thumbContainer);
		}
		
		//{ region Getters
		
		[Inspectable(defaultValue="...")]
		public function get path():String {	return _path; }
		public function set path(value:String):void { 
			_path = value; 
			_repository = new GalleryRepository(_path, _specialLoadingStructure);
			_repository.addEventListener(Event.COMPLETE, repository_Complete);
			this.dispatchEvent(new LoadingEvent(LoadingEvent.SHOW_PROGRESS_ICON));
		}
		
		[Inspectable(defaultValue="right", type="list", enumeration="right,left,up,down")]
		public function get thumbnailsPosition():String { return _thumbnailsPosition; }
		public function set thumbnailsPosition(value:String):void {	
			_thumbnailsPosition = value; 
		}
		
		[Inspectable(defaultValue=200)]
		public function get thumbnailsContainerWidth():Number {	return _thumbnailsContainerWidth; }
		public function set thumbnailsContainerWidth(value:Number):void { 
			_thumbnailsContainerWidth = value; 
			configLayout();
		}
		
		[Inspectable(defaultValue=500)]
		public function get thumbnailsContainerHeight():Number { return _thumbnailsContainerHeight; }
		public function set thumbnailsContainerHeight(value:Number):void { 
			_thumbnailsContainerHeight = value; 
			configLayout();
		}
		
		//} endregion
	}

}